package cn.gailvlun.gll.presentation.apartment.task;

import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;

import cn.gailvlun.gll.R;
import cn.gailvlun.gll.net.apartment.task.TaskRecord;
import cn.gailvlun.gll.presentation.base.BaseActivity;
import cn.gailvlun.gll.databinding.ActivityCheckInBinding;
import cn.gailvlun.gll.net.HttpMethods;
import cn.gailvlun.gll.net.HttpSubscriber;
import cn.gailvlun.gll.net.apartment.room.ApartmentTask;
import cn.gailvlun.gll.net.apartment.task.AddCheckInReq;
import cn.gailvlun.gll.presentation.chat.ChatActivity;
import cn.gailvlun.gll.util.DialogUtil;
import cn.gailvlun.gll.util.IsEmpty;
import cn.gailvlun.gll.util.RxUtil;
import cn.gailvlun.gll.util.ToastUtil;
import cn.gailvlun.gll.widget.NavigationBarView;



public class CheckInActivity extends BaseActivity {

    public static void openActivity(Context context, ApartmentTask task) {
        Intent intent = new Intent(context, CheckInActivity.class);
        intent.putExtra("taskId", task.getId());
        intent.putExtra("roomId", task.getRoom());
        context.startActivity(intent);
    }

    private ActivityCheckInBinding mBinding;
    private AddCheckInReq addCheckInReq = new AddCheckInReq();

    @Override
    protected void initComponent() {
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_check_in);
        mBinding.setPresenter(new Presenter());
    }

    @Override
    protected void createHandler() {
        mBinding.nbv.setOnBackListener(new NavigationBarView.OnBackListener() {
            @Override
            public void onBack(NavigationBarView nb) {
                onBackPressed();
            }
        });
    }



    @Override
    protected void loadData(@Nullable Bundle savedInstanceState) {
        Integer taskId = getIntent().getIntExtra("taskId", -1);

        if (taskId != -1) {
            addCheckInReq.setTask_id(taskId);
            mBinding.setItem(addCheckInReq);
        }
    }

    public class Presenter {
        public void upload() {
            if (IsEmpty.string(mBinding.getItem().getText())) {
                
                ToastUtil.showShort("请填写内容");
                return;
            }
            if (mBinding.pp.getFileList().size() == 0) {
                
                ToastUtil.showShort("请选择图片");
                return;
            }
            addCheckInReq.setPic(mBinding.pp.getFileList().get(0).getOssKey());
            HttpMethods.getTaskService().addCheckIn(addCheckInReq)
                    .compose(RxUtil.<TaskRecord>applyScheduler())
                    .subscribe(new HttpSubscriber<TaskRecord>() {
                        @Override
                        protected void onSuccess(TaskRecord response) {
                            ChatActivity.openActivity(mContext, "room:" + response.getMember().getRoom(), false);
                        }

                        @Override
                        protected void onFailure(String errMsg, @Nullable TaskRecord response, int code) {
                            DialogUtil.showError(mContext, errMsg);
                        }
                    });
        }
    }
}
